/*
 * $Id: ether.c 50 2009-02-10 05:48:05Z hyunghwan.chung $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#include <xp/net/ether.h>
#include <xp/bas/ctype.h>
#include <xp/bas/stdlib.h>
#include <xp/bas/string.h>
#include <xp/bas/memory.h>

#define XDIGIT_TO_NUM(c) \
	(((c) >= XP_T('0') && (c) <= XP_T('9'))? ((c) - XP_T('0')): \
	 ((c) >= XP_T('A') && (c) <= XP_T('F'))? ((c) - XP_T('A') + 10): \
	 ((c) >= XP_T('a') && (c) <= XP_T('f'))? ((c) - XP_T('a') + 10): -1)

int xp_strtoethwaddr (const xp_char_t* str, xp_ethwaddr_t* hwaddr)
{
	xp_size_t idx = 0;
	const xp_char_t* p = str;
	int tmp;

	/*while (xp_isspace(*p)) p++;*/

	while (*p != XP_T('\0')) 
	{
		tmp = XDIGIT_TO_NUM (*p);
		if (tmp <= -1) return -1;
		hwaddr->value[idx] = hwaddr->value[idx] * 16 + tmp;
		p++;

		tmp = XDIGIT_TO_NUM (*p);
		if (tmp <= -1) return -1;
		hwaddr->value[idx] = hwaddr->value[idx] * 16 + tmp;
		p++;

		if (idx < XP_ETHWADDR_LEN - 1) 
		{
			if (*p != XP_T(':')) return -1;
			p++; idx++;
		}
		else 
		{
			return (*p == XP_T('\0'))? 0: -1;
		}
	}

	return -1;
}

int xp_strxtoethwaddr (const xp_char_t* str, xp_size_t len, xp_ethwaddr_t* hwaddr)
{
	xp_size_t idx = 0;
	const xp_char_t* p = str, * end = str + len;
	int tmp;

	/*while (xp_isspace(*p)) p++;*/

	while (p < end)
	{
		tmp = XDIGIT_TO_NUM (*p);
		if (tmp <= -1) return -1;
		hwaddr->value[idx] = hwaddr->value[idx] * 16 + tmp;
		p++;

		if (p >= end) return -1;
		tmp = XDIGIT_TO_NUM (*p);
		if (tmp <= -1) return -1;
		hwaddr->value[idx] = hwaddr->value[idx] * 16 + tmp;
		p++;

		if (idx < XP_ETHWADDR_LEN - 1) 
		{
			if (p >= end || *p != XP_T(':')) return -1;
			p++; idx++;
		}
		else 
		{
			return (p >= end)? 0: -1;
		}

	}

	return -1;
}

xp_size_t xp_ethwaddrtostrx (
	const xp_ethwaddr_t* hwaddr, xp_char_t* buf, xp_size_t size)
{
	xp_size_t i;
	xp_char_t tmp[XP_ETHWADDR_LEN][3];
	
	if (size <= 0) return 0;

	for (i = 0; i < XP_ETHWADDR_LEN; i++) 
	{
		xp_bytetostr_upper (hwaddr->value[i], 
			tmp[i], xp_countof(tmp[i]), 16, XP_T('0'));
	}

	return xp_strxapp (buf, size, 
		tmp[0], XP_T(":"), tmp[1], XP_T(":"),
		tmp[2], XP_T(":"), tmp[3], XP_T(":"),
		tmp[4], XP_T(":"), tmp[5], XP_NULL);
}

int xp_ether_aton (const xp_char_t* str, xp_byte_t hw[XP_ETHWADDR_LEN])
{
	int n;
	xp_ethwaddr_t hwaddr;
	n = xp_strtoethwaddr (str, &hwaddr);
	if (n == 0) xp_memcpy (hw, hwaddr.value, XP_SIZEOF(hwaddr.value));
	return n;
}

int xp_ether_xaton (
	const xp_char_t* str, xp_size_t len, xp_byte_t hw[XP_ETHWADDR_LEN])
{
	int n;
	xp_ethwaddr_t hwaddr;
	n = xp_strxtoethwaddr (str, len, &hwaddr);
	if (n == 0) xp_memcpy (hw, hwaddr.value, XP_SIZEOF(hwaddr.value));
	return n;
}

xp_size_t xp_ether_ntoa (
	const xp_byte_t hw[XP_ETHWADDR_LEN], 
	xp_char_t* buf, xp_size_t size)
{
	xp_ethwaddr_t hwaddr;
	xp_memcpy (hwaddr.value, hw, XP_SIZEOF(hwaddr.value));
	return  xp_ethwaddrtostrx (&hwaddr, buf, size);
}
